import java.util.ArrayDeque;
import java.util.Deque;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode root = null;
        for (int num : nums) {
            TreeNode cur = new TreeNode(num);
            TreeNode curLeft = null;
            while (!stack.isEmpty() && stack.peek().val < num) {
                curLeft = stack.pop();
            }
            if (!stack.isEmpty()) {
                stack.peek().right = cur;
            }
            cur.left = curLeft;

            if (stack.isEmpty()) {
                root = cur;
            }
            stack.push(cur);
        }
        return root;
    }
}